<#list serviceInfo.columns as item>
<#if item.prikey>
<#assign priname="${item.javaCode}"/>
</#if>
</#list>
layui.define(['table','laydate','layer','form'],function(exports){ //提示：模块也可以依赖其它模块，如：layui.define('layer', callback);
  /* 变量定义 */
  	var $ = layui.$,
  	table = layui.table,
  	layer = layui.layer,
  	form = layui.form,
  	laydate = layui.laydate,
  	windowSize = {width:'700px',height:'600px'},
  	searchWindowSize = {width:'1024px',height:'468px'},
  	optName;
  	
  	/** 定义table **/
  	var tableObj = table.render({
  	  id: 'main-table',
  	  elem: '#table', //指定原始表格元素选择器（推荐id选择器）
//   	  contentType: 'application/json',
  	  url: '/${urlPrefix}/${baseUrl}/query_list',
  	  method:'post',
  	  autoSort : false,
  	  totalRow: false,
  	  size: 'sm',
	  even: true,
  	  toolbar: '#toolbar',
  	  defaultToolbar:['filter'],
  	  height: 'full-70',
  	  limits : [10,30,50,100],
  	  limit: 30,
  	  page: true,
  	  cols: [[
  		  {type:'checkbox'},
  		  <#list serviceInfo.columns as columnInfo>
  		  <#if (columnInfo.columnRemark?string != "")>
  		  	<#assign remark="${columnInfo.columnRemark}"/>
  		  <#else>
  		  	<#assign remark="${columnInfo.javaCode}"/>
  		  </#if>
  		  <#switch columnInfo.javaType>
  		  <#case "Date">
  		  	<#assign columnWidth=150>
  		    <#break>
	      <#case "Boolean">
	      <#assign columnWidth=100>
  		    <#break>
  		  <#case "Integer">
  		  <#case "Long">
  		  <#case "Double">
  		  <#case "Float">
		      <#assign columnWidth=70>
    		    <#break>
  		  <#default>
  		  	<#assign columnWidth=200>
  		  </#switch>
  		  {field: '${columnInfo.javaCode}', title: '${remark}',sort:false,width:${columnWidth},templet: function(d){
  			  switch(d.${columnInfo.javaCode}) {
  			  	<#if (columnInfo.javaType == "Boolean")>
  			  	case true:
  			  		return '是';
  			  	case false:
  			  		return '否';
  			  	<#else>
  			  	case null:
  			  		return '--';
  			  	case '':
  			  		return '';
  			  	</#if>
  			  	default:
  			  		return d.${columnInfo.javaCode};
  			  }
  	      }},
  		  </#list>
  		  {fixed: 'right', title:'操作', toolbar: '#optbar', width:165}
  	  ]]
  	});
  	
  	/** 顶部toolbar事件 **/
  	table.on('toolbar(table)', function(obj){
  		switch(obj.event){
  		  case 'refresh-opt':
			    tableObj.reload();
			  break;
  		  case 'addData-opt':
  			  var data = obj.data;
  			  optName = 'new';
  			  var layerindex = layer.open({
  				  type: 1,
  				  area: [windowSize.width,windowSize.height],
  				  title : '新增/添加',
  				  content: $('#windows'),
  				  resize : false,
  				  move : false,
  				  maxmin: false,
  				  shadeClose : false,
  				  btn : [ '确定', '取消' ],
  				  success : function(layero, index){
  					  $('#win-form')[0].reset();
  				  },
				  yes : function(index, layero) {
					  $('#win-submit').trigger('click');
				  }
  				});
  			  //layer.full(layerindex);
	      break;
  		  case 'batch-del-opt':
  			  var checkStatus = table.checkStatus('main-table');
  			  if(checkStatus.data.length ==0){
  				  layer.alert('请先选择待删除行',{icon: 5});
  				  return;
  			  }
  			  layer.confirm("你确定批量删除"+checkStatus.data.length+"个项目吗？", {icon: 3, title:'提示'},
	            function(index){//确定回调
			  		var keys = new Array();
			  		$.each(checkStatus.data,function(i,item){
			  			keys.push(item.${priname});
			  		});
			  		batchDelete(keys,index);
	            },function (index) {//取消回调
	               layer.close(index);
	            }
		      );
  		  break;
  		}
  	});
  	
  	/** Windows表单提交事件 **/
  	form.on('submit(win-submit)', function(data){
		  $.ajax({
			    type: 'POST',
			    url: optName=='new'?'/${urlPrefix}/${baseUrl}/add_item':'/${urlPrefix}/${baseUrl}/update_item',
			    data: $(data.form).serializeArray(),
			    success: function (data) {
		        if(data.success){
		        	layer.closeAll();
		        	tableObj.reload();
		        	layer.msg(data.message);
		        }else{
		            layer.msg(data.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
		        }
			    },
			    error: function(data) {
			        layer.msg(data.responseJSON.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
			    },
			    dataType: "json"
			});
		  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		});
  	
  	/** 搜索框搜索事件 **/
  	form.on('submit(search-opt)',function(data){
  		tableObj.reload({
  			where:data.field,
  			page: {
				curr: 1
			}
  		});
  	    return false;
  	});
  	
  	/** 右侧tool按钮事件 **/
  	table.on('tool(table)', function(obj){
  		var data = obj.data;
  		switch(obj.event){
  		  case 'edit-opt':
  			  optName = 'update';
  			  var layerindex = layer.open({
  				  type: 1,
  				  area: [windowSize.width,windowSize.height],
  				  title : '编辑',
  				  content: $('#windows'),
  				  shadeClose : false,
  				  move : false,
  				  maxmin: false,
  				  btn : [ '确定', '取消' ],
  				  success : function(layero, index){
  					  $('#win-form')[0].reset();
  					  $.post(
						  "/${urlPrefix}/${baseUrl}/get_item_by_key",
						  {${priname}:data.${priname}},
						  function(data, textStatus, jqXHR){
							  form.val("win-form",data);
						  },"json");
  				  },
				  yes : function(index, layero) {
					  $('#win-submit').trigger('click');
				  }
  				});
  			  	//layer.full(layerindex);
  			  break;
  		  case 'view-opt':
  			  optName = 'view';
  			  var layerindex = layer.open({
  				  type: 1,
  				  area: [windowSize.width,windowSize.height],
  				  title : '查看',
  				  content: $('#windows'),
  				  shadeClose : false,
  				  move : false,
  				  maxmin: false,
  				  btn : [ '关闭'],
  				  success : function(layero, index){
  					  $('#win-form')[0].reset();
  					  $.post(
						  "/${urlPrefix}/${baseUrl}/get_item_by_key",
						  {${priname}:data.${priname}},
						  function(data, textStatus, jqXHR){
							  form.val("win-form",data);
						  },"json");
  				  },
  				  btn1 : function(index, layero) {
  					  layer.closeAll();
				  }
  				});
  			  	//layer.full(layerindex);
  			  break;
  		  case 'del-opt':
  			  layer.confirm("你确定删除项目“"+obj.data.id+"”吗？", {icon: 3, title:'提示'},
			            function(index){//确定回调
    				  		var keys = new Array();
    				  		keys.push(obj.data.${priname});
    				  		batchDelete(keys,index);
			            },function (index) {//取消回调
			               layer.close(index);
			            }
			        );
  			  break;
  		}
  	});
  	
  	table.on('sort(table)', function(obj){
	  table.reload('main-table', {
	    initSort: obj,
	    where: {
	      orderField: obj.field,
	      orderType: obj.type
	    }
	  });
	});
  	
  	/** 搜索更多 **/
  	$('#search-more').click(function(){
  		layer.open({
			  type: 1,
			  area: [searchWindowSize.width,searchWindowSize.height],
			  title : '更多查询条件',
			  content: $('#windows-search'),
			  resize : true,
			  shadeClose : true,
			  shade : false,
			  maxmin: false,
			  btn : [ '查询', '关闭' ],
			  success : function(layero, index){
			  },
			  yes : function(index, layero) {
				tableObj.reload({where:form.val('win-search-form')});
			  }
			});
  	});
  	
  	function batchDelete(keys,index){
  		$.ajax({
                type: 'POST',
                url: '/${urlPrefix}/${baseUrl}/delete_item',
                data: JSON.stringify(keys),
                success: function (data) {
                    if(data.success){
                        layer.close(index);
                    }else{
                        layer.msg(data.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
                    }
                    tableObj.reload();
                    layer.msg(data.message);
                },
                error: function(data) {
                    layer.msg(data.responseText,{icon: 5,anim: 6,title:'错误',time: 0,btn:['确定']});
                },
                contentType: "application/json",
                dataType: "json"
            });
  	}
  	
  	<#list serviceInfo.columns as columnInfo>
  	<#if (columnInfo.dataType == "datetime")||(columnInfo.dataType == "timestamp")>
  	/** 搜索框日历控件渲染___开始 **/
  	var ${columnInfo.javaCode}${columnInfo.TIMESTARTSTR} = laydate.render({
	    elem: '#${columnInfo.javaCode}${columnInfo.TIMESTARTSTR}',
	    type: 'datetime',
	    calendar: true,
	    done: function(value, date){
	    	// 日历选择后触发事件
	    	${columnInfo.javaCode}${columnInfo.TIMEENDSTR}.config.min = {                    
                      year:date.year,
                      month:date.month-1,//关键
                      date:date.date,
                      hours:date.hours,
                      minutes:date.minutes,
                      seconds:date.seconds
                  };
	    }
	  });
  	var ${columnInfo.javaCode}${columnInfo.TIMEENDSTR} = laydate.render({
	    elem: '#${columnInfo.javaCode}${columnInfo.TIMEENDSTR}',
	    type: 'datetime',
	    calendar: true,
	    done: function(value, date){
	    	if(value == ""){
	    		${columnInfo.javaCode}${columnInfo.TIMESTARTSTR}.config.max = {
	    			year:2099
	    		}
	    	} else {
		    	${columnInfo.javaCode}${columnInfo.TIMESTARTSTR}.config.max = {                    
                    year:date.year,
                    month:date.month-1,//关键
                    date:date.date,
                    hours:date.hours,
                    minutes:date.minutes,
                    seconds:date.seconds
                };
	    	}
	    }
	  });
  	/** 搜索框日历控件渲染___结束 **/
  	
  	/** Window窗口日历控件渲染___开始 **/
  	var ${columnInfo.javaCode}${columnInfo.TIMESTARTSTR}_win = laydate.render({
	    elem: '#${columnInfo.javaCode}${columnInfo.TIMESTARTSTR}-win',
	    type: 'datetime',
	    calendar: true,
	    done: function(value, date){
	    	${columnInfo.javaCode}${columnInfo.TIMEENDSTR}_win.config.min = {                    
                  year:date.year,
                  month:date.month-1,//关键
                  date:date.date,
                  hours:date.hours,
                  minutes:date.minutes,
                  seconds:date.seconds
              };
	    }
	  });
  	var ${columnInfo.javaCode}${columnInfo.TIMEENDSTR}_win = laydate.render({
	    elem: '#${columnInfo.javaCode}${columnInfo.TIMEENDSTR}-win',
	    type: 'datetime',
	    calendar: true,
	    done: function(value, date){
	    	if(value == ""){
	    		${columnInfo.javaCode}${columnInfo.TIMESTARTSTR}_win.config.max = {
	    			year:2099
	    		}
	    	} else {
	    		${columnInfo.javaCode}${columnInfo.TIMESTARTSTR}_win.config.max = {                    
                    year:date.year,
                    month:date.month-1,//关键
                    date:date.date,
                    hours:date.hours,
                    minutes:date.minutes,
                    seconds:date.seconds
                };
	    	}
	    }
	  });
  	/** Window窗口日历控件渲染___结束 **/
  	laydate.render({
	    elem: '#${columnInfo.javaCode}-win',
	    type: 'datetime',
	    calendar: true
	  });
  	</#if>
  	</#list>
  function submitJson(formdata){
  	var fields = $(formdata).serializeArray();
	var obj = {}; //声明一个对象
	$.each(fields, function (index, field) {
	    obj[field.name] = field.value; //通过变量，将属性值，属性一起放到对象中
	})
	return JSON.stringify(obj);
  }
  //输出${serviceInfo.serviceName}Page接口
  exports('${serviceInfo.serviceName}Page', {});
});